import request from "@/utils/request";

/**
 * 获取订单列表
 */
export function getOrderList(params: any) {
	// 确保传入参数中有userId，如果没有则从用户存储中获取
	return request.get("/api/orders/list", params);
}

/**
 * 获取订单详情
 */
export function getOrderDetail(id: number) {
	return request.get(`/api/orders/findOne/${id}`);
}

/**
 * 创建订单
 */
export interface OrderItem {
	productId: number;
	quantity: number;
}

export interface CreateOrderParams {
	userId: number;
	items: OrderItem[];
	receiver: string;
	phone: string;
	address: string;
	remark?: string;
}

export function createOrder(data: any) {
	return request.post("/api/orders/create", data);
}

/**
 * 支付订单
 */
export function payOrder(id: number) {
	return request.post(`/api/orders/${id}/pay`);
}

/**
 * 取消订单
 */
export function cancelOrder(id: number) {
	return request.post(`/api/orders/${id}/cancel`);
}

/**
 * 确认收货
 */
export function confirmOrder(id: number) {
	return request.post(`/api/orders/${id}/confirm`);
}
